package com.translate.web.api.controller;

import java.io.IOException;
import java.util.Map;
import java.util.function.Function;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.response.vo.ResponseModel;
import com.returns.value.ICodeAndData;
import com.translate.web.config.OssConfig;
import com.translate.web.config.SpringBeanUtil;
import com.translate.web.config.UploadTypeConf;
import com.translate.web.param.FileData;
import com.translate.web.service.upload.IUploadService;

@RestController
@RequestMapping("upload")
public class UploadController {

	private IUploadService uploadService;

	private UploadTypeConf uploadTypeConf;

	public UploadController(UploadTypeConf typeConf) {
		this.uploadTypeConf = typeConf;
	}

	@PostMapping("comment")
	public ResponseModel<String> comment(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getOrder);

	}
	
	@PostMapping("order")
	public ResponseModel<String> order(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getOrder);

	}
	
	@PostMapping("orderApproval")
	public ResponseModel<String> orderApproval(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getOrderApproval);

	}

	@PostMapping("paymentApproval")
	public ResponseModel<String> paymentApproval(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getPaymentApproval);

	}
	
	@PostMapping("productImag")
	public ResponseModel<String> productImag(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getProductImag);

	}
	
	@PostMapping("receivables")
	public ResponseModel<String> receivables(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getReceivables);

	}
//	@PostMapping("sts")
//	public ResponseModel<AssumeRoleResponse> sts(HttpServletRequest httpServletRequest) throws Exception {
//		
//		uploadService = getIUploadService(httpServletRequest);
//
//		
//		ICodeAndData<AssumeRoleResponse> stsUpload = this.uploadService.stsUpload(new byte[] {}, "");
//		return ResponseModel.success(stsUpload.getData());
//		
//	}
	/**
	 * 上传稿件
	 * @param fileData
	 * @param httpServletRequest
	 * @return
	 * @throws Exception
	 */
	@PostMapping("signURLByManuscript")
	public ResponseModel<Map<String, Object>> signURLByManuscript(@RequestBody @Validated FileData fileData, HttpServletRequest httpServletRequest) throws Exception {
		
		IUploadService uploadService = getIUploadService(httpServletRequest);

		ICodeAndData<Map<String, Object>> stsUpload = uploadService.signURL(fileData.getFileName(), OssConfig::getOrder);
		return ResponseModel.success(stsUpload.getData());
		
	}
	
	
	/**
	 * 
	 * @param multipartFile
	 * @param httpServletRequest
	 * @return
	 * @throws IOException
	 */
	private <V> ResponseModel<String> upload(MultipartFile multipartFile, HttpServletRequest httpServletRequest,
			Function<V, String> function) {

		try {

			uploadService = getIUploadService(httpServletRequest);

			ICodeAndData<String> codeAndData = uploadService.uploadFileToOSS(multipartFile.getInputStream(),
					multipartFile.getOriginalFilename(), function, multipartFile.getSize());

			return ResponseModel.<String>response(codeAndData);
		} catch (Exception e) {

			throw new Error(e);
		}
	}
	
	private IUploadService getIUploadService(HttpServletRequest httpServletRequest) throws Exception {
			String header = httpServletRequest.getHeader("storeType");
			
			if (null == header) {
				throw new IllegalArgumentException("storeType 不能为空");
			}
			
			int type = Integer.valueOf(header);

			return SpringBeanUtil.getBean(this.uploadTypeConf.getType()[type]);

	}

	@PostMapping("uploadHeader")
	public ResponseModel<String> uploadHeader(MultipartFile multipartFile, HttpServletRequest httpServletRequest) {

		return upload(multipartFile, httpServletRequest, OssConfig::getUserPrefix);
	}
}